<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creates a new DateInterval object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.dateinterval.html">« DateInterval</a></li>
      <li style="float: right;"><a href="dateinterval.createfromdatestring.html">DateInterval::createFromDateString »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.dateinterval.html">DateInterval</a></li>
    <li>Creates a new DateInterval object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="dateinterval.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateInterval::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">DateInterval::__construct</span> &mdash; <span class="dc-title">Creates a new DateInterval object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-dateinterval.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>DateInterval::__construct</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$duration</code></span>)</div>

  <p class="para rdfs-comment">
   Creates a new DateInterval object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-dateinterval.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">duration</code></dt>

     <dd>

      <p class="para">
       An interval specification.
      </p>
      <p class="para">
       The format starts with the letter <code class="literal">P</code>,
       for <q class="quote">period.</q>
       Each duration period is represented by an integer value
       followed by a period designator.
       If the duration contains time elements, that portion
       of the specification is preceded by the letter
       <code class="literal">T</code>.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>
         <code class="parameter">duration</code> Period Designators
        </strong></caption>
        
         <thead>
          <tr>
           <th>Period Designator</th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><code class="literal">Y</code></td>
           <td>years</td>
          </tr>

          <tr>
           <td><code class="literal">M</code></td>
           <td>months</td>
          </tr>

          <tr>
           <td><code class="literal">D</code></td>
           <td>days</td>
          </tr>

          <tr>
           <td><code class="literal">W</code></td>
           <td>
            weeks. Converted into days.
            Prior to PHP 8.0.0, can not be combined with <code class="literal">D</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">H</code></td>
           <td>hours</td>
          </tr>

          <tr>
           <td><code class="literal">M</code></td>
           <td>minutes</td>
          </tr>

          <tr>
           <td><code class="literal">S</code></td>
           <td>seconds</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       Here are some simple examples.
       Two days is <code class="literal">P2D</code>.
       Two seconds is <code class="literal">PT2S</code>.
       Six years and five minutes is <code class="literal">P6YT5M</code>.
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        The unit types must be entered from the largest
        scale unit on the left to the smallest scale unit
        on the right.
        So years before months, months before days,
        days before minutes, etc.
        Thus one year and four days must be represented as
        <code class="literal">P1Y4D</code>, not <code class="literal">P4D1Y</code>.
       </p>
      </p></blockquote>
      <p class="para">
       The specification can also be represented as a date time.
       A sample of one year and four days would be
       <code class="literal">P0001-00-04T00:00:00</code>.
       But the values in this format can not exceed a given period&#039;s
       roll-over-point (e.g. <code class="literal">25</code> hours is invalid).
      </p>
      <p class="para">
       These formats are based on the <a href="http://en.wikipedia.org/wiki/Iso8601#Durations" class="link external">&raquo;&nbsp;ISO 8601 duration
       specification</a>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-dateinterval.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws an <span class="classname"><a href="class.exception.html" class="classname">Exception</a></span> when the <code class="parameter">duration</code>
   cannot be parsed as an interval.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-dateinterval.construct-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       Only the <code class="literal">y</code> to <code class="literal">f</code>,
       <code class="literal">invert</code>, and <code class="literal">days</code> will be visible,
       including a new <code class="literal">from_string</code> boolean property.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="literal">W</code> can be combined with <code class="literal">D</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-dateinterval.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2052">
    <p><strong>示例 #1 Constructing and using <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> objects</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;specific&nbsp;date<br /></span><span style="color: #0000BB">$someDate&nbsp;</span><span style="color: #007700">=&nbsp;\</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d&nbsp;H:i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"2022-08-25&nbsp;14:18"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;interval<br /></span><span style="color: #0000BB">$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">"P7D"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Add&nbsp;interval<br /></span><span style="color: #0000BB">$someDate</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Convert&nbsp;interval&nbsp;to&nbsp;string<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$interval</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">"%d"</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen"><br />
7<br />
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2053">
    <p><strong>示例 #2 <span class="classname"><a href="class.dateinterval.html" class="classname">DateInterval</a></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$interval&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DateInterval</span><span style="color: #007700">(</span><span style="color: #DD0000">'P1W2D'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$interval</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 8.2 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(DateInterval)#1 (10) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(9)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;from_string&quot;]=&gt;
  bool(false)
}
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(DateInterval)#1 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(9)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(0)
  [&quot;special_amount&quot;]=&gt;
  int(0)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(0)
}
</pre></div>
    </div>
    <div class="example-contents"><p>以上例程在 PHP 7 中的输出：</p></div>
    <div class="example-contents screen">
     <div class="cdata"><pre>
object(DateInterval)#1 (16) {
  [&quot;y&quot;]=&gt;
  int(0)
  [&quot;m&quot;]=&gt;
  int(0)
  [&quot;d&quot;]=&gt;
  int(2)
  [&quot;h&quot;]=&gt;
  int(0)
  [&quot;i&quot;]=&gt;
  int(0)
  [&quot;s&quot;]=&gt;
  int(0)
  [&quot;f&quot;]=&gt;
  float(0)
  [&quot;weekday&quot;]=&gt;
  int(0)
  [&quot;weekday_behavior&quot;]=&gt;
  int(0)
  [&quot;first_last_day_of&quot;]=&gt;
  int(0)
  [&quot;invert&quot;]=&gt;
  int(0)
  [&quot;days&quot;]=&gt;
  bool(false)
  [&quot;special_type&quot;]=&gt;
  int(0)
  [&quot;special_amount&quot;]=&gt;
  int(0)
  [&quot;have_weekday_relative&quot;]=&gt;
  int(0)
  [&quot;have_special_relative&quot;]=&gt;
  int(0)
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-dateinterval.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="dateinterval.format.html" class="function" rel="rdfs-seeAlso">DateInterval::format()</a> - Formats the interval</span></li>
    <li class="member"><span class="function"><a href="datetime.add.html" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - 修改 DateTime 对象，增加天、月、年、小时、分钟以及秒的数量。</span></li>
    <li class="member"><span class="function"><a href="datetime.sub.html" class="function" rel="rdfs-seeAlso">DateTime::sub()</a> - 对 DateTime 对象减去一定量的日、月、年、小时、分钟和秒。</span></li>
    <li class="member"><span class="function"><a href="datetime.diff.html" class="function" rel="rdfs-seeAlso">DateTime::diff()</a> - 返回两个 DateTime 之间的差值</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>